<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------
//Ellipsize String的重写
/**
 * Ellipsize String
 *
 * This function will strip tags from a string, split it at its max_length and ellipsize
 *
 * @param	string		string to ellipsize
 * @param	integer		max length of string
 * @param	mixed		int (1|0) or float, .5, .2, etc for position to split
 * @param	string		ellipsis ; Default '...'
 * @return	string		ellipsized string
 */
if ( ! function_exists('ellipsize'))
{
	function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
	{
		// Strip tags
		$str = trim(strip_tags($str));

		// Is the string long enough to ellipsize?
		if (mb_strlen($str) <= $max_length)
		{
			return $str;
		}

		$beg = mb_substr($str, 0, floor($max_length * $position));

		$position = ($position > 1) ? 1 : $position;

		if ($position === 1)
		{
			$end = mb_substr($str, 0, -($max_length - mb_strlen($beg)));
		}
		else
		{
			$end = mb_substr($str, -($max_length - mb_strlen($beg)));
		}

		return $beg.$ellipsis.$end;
	}
}

// ------------------------------------------------------------------------
//json_encode 的扩展
/**
 * json_encode_jsfunc
 *
 * This function will made it support to encode php array into json object without add quotes to js function
 *
 * @param	array		the array need to be encoded
 * @param	array		the key's value that need not to be quoted
 */
if ( ! function_exists('json_encode_jsfunc'))
{
	function json_encode_jsfunc($input = array(), $no_quotes_key = array(), $funcs = array(), $level = 0) {
		foreach ($input as $key => $value) {
			if (is_array($value)) {
				$ret = json_encode_jsfunc($value, $no_quotes_key, $funcs, 1);
				$input[$key] = $ret[0];
				$funcs = $ret[1];
			} else {
				if (in_array($key, $no_quotes_key) || substr($value, 0, 10) == 'function()') {
					$func_key = "#" . uniqid() . "#";
					$funcs[$func_key] = $value;
					$input[$key] = $func_key;
				}
			}
		}
		
		if ($level == 1) {
			return array($input, $funcs);
		} else {
			$input_json = json_encode($input);
			foreach($funcs as $key => $value) {
				$input_json = str_replace('"' . $key . '"', $value, $input_json);
			}
			return $input_json;
		}
	}
}

if ( ! function_exists('selector_replace'))
{
	function selector_replace($selector_str) {
		return preg_replace('/([#. ])/', '_', $selector_str);
	}
}
